#!/bin/bash
set -e

export inner_build_kern_fname=002.inner_build_kern.sh

if [ "x${1}" = "xsubbuild" ]; then
	#如果是上层调用该脚本
	if [ $# -eq 1 ]; then
		#功能码999表示查看帮助
		export fun_code=999
	elif [ $# -eq 2 ]; then
		#获取功能码
		export fun_code=$2
	else
    	echo -e "\033[47;41m子模块参数错误!\033[0m"
    	echo -e "\033[47;41m子模块参数参考: $0 subbuild 调用码 功能码\033[0m"
		exit 1
	fi
    echo -e "\033[30;43m继承环境变量!\033[0m"
else
    echo -e "\033[30;43m配置环境变量!\033[0m"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh > /dev/null 
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh > /dev/null 
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh > /dev/null 
	#-------------------------------------------------------------------------------------

	#检查当前机型配置
	if [ ! -f ${CURCFG_FILE} ]; then
		serro "检查当前配置文件... [失败]"
		exit 1
	fi
	source ${CURCFG_FILE} > /dev/null 
	
	#检测当前环境
	check_env

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------

	if [ $# -eq 1 ]; then
		export fun_code=$1
	else
		export fun_code=999
	fi
fi

#刷新环境参数
update_copy_env
update_kern_env
update_kern_env
update_knfs_env
update_rtfs_env

echo -e "\033[47;42m$0:当前功能码: ${fun_code}\033[0m"

#-----------------------------------------------------------------------
#模块名称
export MODNAME=${OEMDEV_KERN_CFG_PREFIX}
#压缩包根目录
export MODPKG_ROOT=${SYSKIT_KERN_PKGDIR_ROOT}
#源码包根目录
export MODSRC_ROOT=${SYSKIT_KERN_RAWDIR_ROOT}
#模块压缩包文件名称
export MODPKG_NAME=${MODNAME}.tar.gz 
#模块源代码目录名称
export MODSRC_NAME=${MODNAME}
#模块压缩包路径
export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
#模块源代码路径
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
#模块编译输出根目录
export MODOUT_ROOT=${SYSKIT_KERN_OUT_PATH}
#模块拷贝输出根目录
export MODCPY_ROOT=${IMGBINS_TOPDIR}/${OEMDEV_CUR_CPUCODE}_${OEMDEV_CUR_APPAREA}_${OEMDEV_CUR_TYPE}

#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
showusage()
{
	echo "使用说明:"
	echo "$0 [功能码]"
	echo "$0  1       ${MODNAME}默认配置"
	echo "$0  2       ${MODNAME}菜单配置"
	echo "$0  3       ${MODNAME}源码编译"
	echo "$0  4       ${MODNAME}清除编译"
	echo "$0  5       ${MODNAME}输出拷贝"
	echo "$0  6       ${MODNAME}源码打包"
	echo "$0  7       ${MODNAME}备份配置"
	echo "$0  8       ${MODNAME}模块编译"
	echo "$0  999     查看本帮助"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
kern_defconfig()
{
	local inner_build_kern_func=kern_defconfig
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	if [ ! -d ${MODOUT_ROOT} ]; then
		mkdir -p ${MODOUT_ROOT}
	fi
	cd ${MODSRC_PATH}

	make O=${MODOUT_ROOT} ${OEMDEV_CUR_CPUCODE}_${OEMDEV_CUR_APPAREA}_${OEMDEV_CUR_TYPE}_kern_defconfig
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:[OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:[ERR]"
	fi
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_menuconfig()
{
	local inner_build_kern_func=kern_menuconfig
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	if [ ! -d ${MODOUT_ROOT} ]; then
		mkdir -p ${MODOUT_ROOT}
	fi
	cd ${MODSRC_PATH}
    make O=${MODOUT_ROOT} menuconfig 
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:[OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:[ERR]"
	fi
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_mkmodules()
{
	local inner_build_kern_func=kern_mkmodules
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	
	if [ ! -d ${MODOUT_ROOT} ]; then
		mkdir -p ${MODOUT_ROOT}
	fi
	cd ${MODSRC_PATH}

	sinfo "${inner_build_kern_fname}:${inner_build_kern_func}:make modules [GO]"
	make O=${MODOUT_ROOT} modules -j${MKCL} 
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:make modules [OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:make modules [ERR]"
	fi

	sinfo "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [GO]"
	midir=${MODOUT_ROOT}/modules.install
	make O=${MODOUT_ROOT} modules_install INSTALL_MOD_PATH=${midir}
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [ERR]"
	fi
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_compile()
{
	local inner_build_kern_func=kern_compile
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	
	if [ ! -d ${MODOUT_ROOT} ]; then
		mkdir -p ${MODOUT_ROOT}
	fi
	cd ${MODSRC_PATH}

	sinfo "${inner_build_kern_fname}:${inner_build_kern_func}:make all [GO]"
	make O=${MODOUT_ROOT} all -j${MKCL} 
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:make all [OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:make all [ERR]"
	fi

	sinfo "${inner_build_kern_fname}:${inner_build_kern_func}:make uImage [GO]"
	make O=${MODOUT_ROOT} uImage -j${MKCL}
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:make uImage [OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:make uImage [ERR]"
	fi

	sinfo "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [GO]"
	midir=${MODOUT_ROOT}/modules.install
	make O=${MODOUT_ROOT} modules_install INSTALL_MOD_PATH=${midir}
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:make modules_install [ERR]"
	fi

	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_clean()
{
	local inner_build_kern_func=kern_clean
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	if [ ! -d ${MODOUT_ROOT} ]; then
		mkdir -p ${MODOUT_ROOT}
	fi
	cd ${MODSRC_PATH}
	make distclean 
	if [ $? -eq 0 ]; then
		sdone "${inner_build_kern_fname}:${inner_build_kern_func}:[OK]"
	else
		serro "${inner_build_kern_fname}:${inner_build_kern_func}:[ERR]"
	fi
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_copy()
{	
	local inner_build_kern_func=kern_copy
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	if [ ! -d ${MODCPY_ROOT} ]; then
		mkdir -p ${MODCPY_ROOT}
	fi
	echo -e "cp $MODOUT_ROOT/arch/arm/boot/uImage\n   ${MODCPY_ROOT}/uImage"
	cp $MODOUT_ROOT/arch/arm/boot/uImage              ${MODCPY_ROOT}/uImage

	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

kern_scfg()
{
	local inner_build_kern_func=kern_scfg
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:+++++++++++++++++++++"
	if [ -f ${SYSKIT_KERN_OUT_PATH}/.config ]; then
		sinfo "保存kernel当前配置文件为OEMDEV默认配置: [开始]"
		echo -e "cp\t${SYSKIT_KERN_OUT_PATH}/.config \n\t${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME}"
		cp ${SYSKIT_KERN_OUT_PATH}/.config ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME}
		if [ $? -eq 0 ]; then
			sdone "保存kernel当前配置文件为OEMDEV默认配置: [完毕]"
		else
			serro "保存kernel当前配置文件为OEMDEV默认配置: [失败]"
		fi
		
		if [ -d ${SYSKIT_KERN_CFG_ROOT} ]; then
			sinfo "保存kernel当前配置文件为当前源码默认配置: [开始]"
			echo -e "cp\t${SYSKIT_KERN_OUT_PATH}/.config \n\t${SYSKIT_KERN_CFG_PATH}"
			cp ${SYSKIT_KERN_OUT_PATH}/.config ${SYSKIT_KERN_CFG_PATH}
			if [ $? -eq 0 ]; then
				sdone "保存kernel当前配置文件为当前源码默认配置: [完毕]"
			else
				serro "保存kernel当前配置文件为当前源码默认配置: [失败]"
			fi
		fi
	fi
	snote "${inner_build_kern_fname}:${inner_build_kern_func}:--------------------"
}

#-----------------------------------------------------------------------

mainproc() 
{
	if [ "x$fun_code" = "x1" ]; then
		check_source
		kern_defconfig
	elif [ "x$fun_code" = "x2" ]; then
		check_source
		kern_menuconfig
	elif [ "x$fun_code" = "x3" ]; then
		check_source
		kern_compile
	elif [ "x$fun_code" = "x4" ]; then
		check_source
		kern_clean
	elif [ "x$fun_code" = "x5" ]; then
		kern_copy
	elif [ "x$fun_code" = "x6" ]; then
		pack_module
	elif [ "x$fun_code" = "x7" ]; then
		kern_scfg
	elif [ "x$fun_code" = "x8" ]; then
		kern_mkmodules
	else
		showusage $@
	fi
}

mainproc $@
